#include <memory>
#include <iostream>

using namespace std;
static shared_ptr<int > & getInstance()
{
	static shared_ptr<int> a (new int(3));
	return a;
}



int main() {
	shared_ptr<int> s = getInstance();
	cout << s.use_count();
	
	return 0;
}








#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <Thread>
#include <time.h>
#include <iostream>
#include <thread>
#include "ThreadPool.h"
#include <sys\stat.h>
#include <fstream>
#include <memory>





 void funvec(const shared_ptr<int> & p)
 {
     vector<shared_ptr<int>>  vecs;
     vecs.push_back(p);
     cout << p.use_count() << endl;
 
 }
 int main() {

     int c = 1;
     shared_ptr<int> s;
     s.reset(&c);
    
     cout << s.use_count() << endl;
     funvec(s);
     //vector<shared_ptr<int>>  vecs;
     //vecs.push_back(s);
     //*s = 2;
     //cout<<vecs[0].use_count()<<endl;
     cout << s.use_count() << endl;
     }










using ptrs= shared_ptr<int >;

void test_para(ptrs s1)
{
	cout << __func__ << endl;

	
	cout << s1.use_count() << endl;
	ptrs curr;
	curr.swap(s1);
	cout << s1.use_count() << endl;
}


void test_para(ptrs * s1)
{
	cout << __func__ << endl;
	cout<<s1->use_count()<<endl;
	ptrs curr;
	curr.swap(*s1);
	cout << s1->use_count() << endl;
	cout << curr.use_count() << endl;  //访问智能指针的成员函数用. 访问智能指针指向对象的成员函数用->

}






//g++  7.4.0

#include <iostream>
#include <memory>
#include <vector>
using namespace std;
  vector<shared_ptr<int>>  vecs;
void funvec(const shared_ptr<int>  p)  //不管这里是不是引用  vector里的计数都会同传入的智能指针一样，假如希望当传入的智能指针参数析构时也析构指向的对象，vector中应该存weak_ptr,
                                     {//如果不希望当传入的智能指针参数析构时也析构指向的对象，vector中可以存shared_ptr
     vecs.push_back(p);
     cout << p.use_count() << endl;
 
 }

void funvec2()
{
     int c = 1;
     shared_ptr<int> s(&c);
     cout << s.use_count() << endl;
     funvec(s);
     cout << s.use_count() << endl;
     cout<<vecs[0].use_count()<<endl;

}
 int main() {

      funvec2();
     //vector<shared_ptr<int>>  vecs;
     //vecs.push_back(s);
     //*s = 2;
     cout<<vecs[0].use_count()<<endl;
     //cout << s.use_count() << endl;
     }








     struct Good : std::enable_shared_from_this<Good> // 注意：继承
{
public:
	std::shared_ptr<Good> getptr() {
		return shared_from_this();
	}
	void test()
	{
		ptr = getptr();
		cout << ptr.use_count() << endl;
		auto raw_ptr = ptr.get();
		ptr.reset();
		cout << ptr.use_count() << endl;
		cout << raw_ptr->cs << endl;

	}
	int cs = 5;
	shared_ptr<Good> ptr;
	~Good() { std::cout << "Good::~Good() called" << std::endl; }
};

/*  为什么这个类用不了

class Good : std::enable_shared_from_this<Good> // 注意：继承
{
public:

	std::shared_ptr<Good> getptr() {
		return shared_from_this();
	}
	void test()
	{
		ptr = getptr();
		cout << ptr.use_count() << endl;
		auto raw_ptr = ptr.get();
		ptr.reset();
		cout << ptr.use_count() << endl;
		cout << ptr->cs << endl;

	}
	int cs = 5;
	shared_ptr<Good> ptr;
	~Good() { std::cout << "Good::~Good() called" << std::endl; }
};

*/
int main()
{

	shared_ptr<Good> psss(new Good) ;
	
	psss->test();
}